﻿using Sunny.UI;
using System.Drawing;
using System.Windows.Forms;

namespace CoreShop.App
{
    public partial class UIClassItem : UIUserControl
    {
        public ClassItemInfo Info { get; }

        public UIClassItem()
        {
            InitializeComponent();

            Info = new ClassItemInfo();
            Text = Info.Field1;
        }

        public UIClassItem(ClassItemInfo info)
        {
            InitializeComponent();
            Info = info;
            Text = info.Field1;
            ForeColor = info.ForeColor;
        }

        private bool selected = false;
        public bool Selected
        {
            get => selected;
            set
            {
                selected = value;
                Invalidate();
            }
        }

        private Color selectedColor = Color.FromArgb(221, 221, 221);
        public Color SelectedColor
        {
            get => selectedColor;
            set
            {
                selectedColor = value;
                Invalidate();
            }
        }

        private int itemHeight = 50;

        public int ItemHeight
        {
            get => itemHeight;
            set
            {
                itemHeight = value;
                Invalidate();
            }
        }

        public void SetMinSize()
        {
            ItemHeight = 30;
            Width = 385;
            Font = new Font("微软雅黑", 11.5f);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (Selected)
            {
                e.Graphics.FillRectangle(SelectedColor, 0, 0, Width, Height);
            }

            if (Info == null) return;

            Size namesize = TextRenderer.MeasureText(Text, Font);

            if (Info.ItemType == 0)
            {
                e.Graphics.DrawString(Text, Font, ForeColor, new Rectangle(15, 0, 80, ItemHeight), ContentAlignment.MiddleRight);

                if (!Info.Field2.IsNullOrEmpty())
                    e.Graphics.DrawString(Info.Field2, Font, ForeColor, new Rectangle(95, 0, 90, ItemHeight), ContentAlignment.MiddleLeft);

                if (!Info.Field3.IsNullOrEmpty())
                    e.Graphics.DrawString(Info.Field3, Font, ForeColor, new Rectangle(185, 0, 80, ItemHeight), ContentAlignment.MiddleRight);

                if (!Info.Field4.IsNullOrEmpty())
                    e.Graphics.DrawString(Info.Field4, Font, ForeColor, new Rectangle(265, 0, 90, ItemHeight), ContentAlignment.MiddleLeft);
            }
            else 
            {
                e.Graphics.DrawString(Text, Font, ForeColor, new Rectangle(10, 0, 80, ItemHeight), ContentAlignment.MiddleLeft);

                if (!Info.Field2.IsNullOrEmpty())
                    e.Graphics.DrawString(Info.Field2, Font, ForeColor, new Rectangle(95, 0, 90, ItemHeight), ContentAlignment.MiddleLeft);

                if (!Info.Field3.IsNullOrEmpty())
                    e.Graphics.DrawString(Info.Field3, Font, ForeColor, new Rectangle(185, 0, 80, ItemHeight), ContentAlignment.MiddleLeft);

                if (!Info.Field4.IsNullOrEmpty())
                    e.Graphics.DrawString(Info.Field4, Font, ForeColor, new Rectangle(265, 0, 90, ItemHeight), ContentAlignment.MiddleLeft);
            }

            int top = ItemHeight;
            if (Info.Tags.Count > 0)
            {
                using (Font font = new Font("微软雅黑", 10.5f))
                {
                    foreach (var item in Info.Tags)
                    {
                        Size size = new Size(Width - 35, ((int)e.Graphics.MeasureString(item, font, Width - 35, StringFormat.GenericTypographic).Height));
                        e.Graphics.DrawString(item, font, Brushes.Gray, new RectangleF(30, top, Width - 35, size.Height));
                        top += size.Height;
                        top += 2;
                    }
                }

                top += 2;
            }

            if (Height != top)
                Height = top;
        }

        private void UIClassItem_Click(object sender, System.EventArgs e)
        {
            //Selected = !selected;
            Selected = true;
            foreach (var item in Parent.GetControls<UIClassItem>())
            {
                if (item.Equals(this)) continue;

                item.Selected = false;
            }
        }
    }
}
